using System;
using System.Web.Mvc;
using iHouse.Common.Const;
using iHouse.Infrastructure;
using iHouse.Model.Data;
using iHouse.Model.Repository.Interface;
using iHouse.Resource;
using iHouse.Web.Infrastructure;
using Microsoft.Practices.Unity;

namespace iHouse.Web.Areas.Admin.Controllers
{
    [Authorize]
    public class PublisherController : Controller
    {
        [Dependency]
        public IPublisherRepository PublisherRespository { get; set; }

        //
        // GET: Get items for jqGrid
        public ActionResult GetGridData([PaingCriteriaBinder]PagingCriteria pagingCriteria)
        {
            return Json(
                PublisherRespository.GetPublishersForList(pagingCriteria), JsonRequestBehavior.AllowGet);
        }

        //
        // POST: /Publisher/Create
        [AcceptVerbs(HttpVerbs.Post)]
        [JsonException]
        public void Create()
        {
            if (string.IsNullOrEmpty(this.HttpContext.Request.Form[BusinessConst.NAME_FIELD]))
            {
                throw new Exception(Resources.ExceptionPublisherWithName);
            }

            Publisher publisher = new Publisher();
            publisher.Name = this.HttpContext.Request.Form[BusinessConst.NAME_FIELD];

            PublisherRespository.CreatePublisher(publisher);
        }

        //
        // POST: /Publisher/Update/5
        [AcceptVerbs(HttpVerbs.Post)]
        [JsonException]
        public void Update(int id)
        {
            if (string.IsNullOrEmpty(this.HttpContext.Request.Form[BusinessConst.NAME_FIELD]))
            {
                throw new Exception(Resources.ExceptionPublisherWithName);
            }

            PublisherRespository.UpdatePublisher(id, this.HttpContext.Request.Form[BusinessConst.NAME_FIELD]);
        }

        //
        //POST: /Publisher/Delete/5
        [AcceptVerbs(HttpVerbs.Post)]
        public void Delete(int id)
        {
            PublisherRespository.DeletePublisher(id);
        }
    }
}
